import proj4 from 'proj4';

const cesium = window.Cesium;
class Epsg4548TilingScheme extends cesium.WebMercatorTilingScheme {
    constructor(options) {
      super();
      this._projection.project = function (cartographic, result) {
        result = WGS84ToEPSG4547(
            cesium.Math.toDegrees(cartographic.longitude),
            cesium.Math.toDegrees(cartographic.latitude)
        )
        return new cesium.Cartesian2(result[0], result[1]);
      }
      this._projection.unproject = function (cartesian, result){
        result = EPSG4547ToWGS84(cartesian.x, cartesian.y)
        return new cesium.Cartographic(cesium.Math.toRadians(result[0]), cesium.Math.toRadians(result[1]))
      }
      this._numberOfLevelZeroTilesX = options.numberOfLevelZeroTilesX
      this._numberOfLevelZeroTilesY = options.numberOfLevelZeroTilesY
      this._rectangleSouthwestInMeters = options.rectangleSouthwestInMeters
      this._rectangleNortheastInMeters = options.rectangleNortheastInMeters
    }
  }

// EPSG 4548 的 Proj4 定义
const epsg4547Def = '+proj=tmerc +lat_0=0 +lon_0=114 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs +type=crs';

// 将 WGS 84（EPSG:4326）坐标转换为 EPSG 4548
function WGS84ToEPSG4547(lon, lat) {
    const wgs84Def = 'EPSG:4326';
    proj4.defs('EPSG:4547', epsg4547Def);
    return proj4(wgs84Def, 'EPSG:4547', [lon, lat]);
}
function EPSG4547ToWGS84(easting, northing) {
    const wgs84Def = 'EPSG:4326';
    proj4.defs('EPSG:4547', epsg4547Def);
    return proj4('EPSG:4547', wgs84Def, [easting, northing]);
}


  export default Epsg4548TilingScheme;